//
//  KSSubmitRefundExplainTableViewCell.swift
//  Supermarket
//
//  Created by liuxm on 2017/9/29.
//  Copyright © 2017年 kushao. All rights reserved.
//

import UIKit
import Photos

protocol PurchaseChoosePhotoDelegate: NSObjectProtocol {
    func choosePhoto()
    func deletePhoto(index: NSInteger)
    func textViewText(text: String)
}

/// 提交退款说明
class KSSubmitRefundExplainTableViewCell: UITableViewCell,UITextViewDelegate {
    
    @IBOutlet weak var firstImage: UIButton!
    @IBOutlet weak var secondImage: UIButton!
    @IBOutlet weak var thirdImage: UIButton!
    @IBOutlet weak var hintLabel: UILabel!
    @IBOutlet weak var firstDeleteBtn: UIButton!
    @IBOutlet weak var secondDeleteBtn: UIButton!
    @IBOutlet weak var thirdDeleteBtn: UIButton!
    private let imageManager = PHCachingImageManager()
    weak var delegate: PurchaseChoosePhotoDelegate?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        firstImage.adjustsImageWhenDisabled = false
        secondImage.adjustsImageWhenDisabled = false
        thirdImage.adjustsImageWhenDisabled = false
    }
    
    @IBAction func choosePhotoEvent(_ sender: UIButton) {
        self.delegate?.choosePhoto()
    }
    
    @IBAction func firstDeleteEvent(_ sender: Any) {
        self.delegate?.deletePhoto(index: 1)
    }
    
    @IBAction func secondDeleteEvent(_ sender: Any) {
        self.delegate?.deletePhoto(index: 2)
    }
    
    @IBAction func thirdDeleteEvent(_ sender: Any) {
        self.delegate?.deletePhoto(index: 3)
    }
    
    func setDataWithModel(photo: [PHAsset],camera: [UIImage]) {
        let photoCount = photo.count + camera.count
        hintLabel.isHidden = (photo.count + camera.count) > 0
        switch photoCount {
        case 0:
            firstImage.setBackgroundImage(UIImage.init(named: "Account_FeedBack_AddPhoto"), for: .normal)
            firstImage.isEnabled = true
            secondImage.isHidden = true
            thirdImage.isHidden = true
            firstDeleteBtn.isHidden = true
            secondDeleteBtn.isHidden = true
            thirdDeleteBtn.isHidden = true
        case 1:
            secondImage.setBackgroundImage(UIImage.init(named: "Account_FeedBack_AddPhoto"), for: .normal)
            firstImage.isEnabled = false
            secondImage.isEnabled = true
            secondImage.isHidden = false
            thirdImage.isHidden = true
            firstDeleteBtn.isHidden = false
            secondDeleteBtn.isHidden = true
            thirdDeleteBtn.isHidden = true
        case 2:
            thirdImage.setBackgroundImage(UIImage.init(named: "Account_FeedBack_AddPhoto"), for: .normal)
            firstImage.isEnabled = false
            secondImage.isEnabled = false
            thirdImage.isEnabled = true
            secondImage.isHidden = false
            thirdImage.isHidden = false
            firstDeleteBtn.isHidden = false
            secondDeleteBtn.isHidden = false
            thirdDeleteBtn.isHidden = true
        case 3:
            firstImage.isEnabled = false
            secondImage.isEnabled = false
            thirdImage.isEnabled = false
            secondImage.isHidden = false
            thirdImage.isHidden = false
            thirdDeleteBtn.isHidden = false
            firstDeleteBtn.isHidden = false
            secondDeleteBtn.isHidden = false
            thirdDeleteBtn.isHidden = false
        default:
            break
        }
        
        if photo.count > 0 {
            for i in 0...photo.count - 1 {
                let asset = photo[i]
                switch i {
                case 0:
                    self.imageManager.requestImage(for: asset, targetSize: CGSize.init(width: 77, height: 77), contentMode: .aspectFit, options: nil) { (image, nfo) in
                        self.firstImage.setBackgroundImage(image, for: .normal)
                    }
                case 1:
                    self.imageManager.requestImage(for: asset, targetSize: CGSize.init(width: 77, height: 77), contentMode: .aspectFit, options: nil) { (image, nfo) in
                        self.secondImage.setBackgroundImage(image, for: .normal)
                    }
                case 2:
                    self.imageManager.requestImage(for: asset, targetSize: CGSize.init(width: 77, height: 77), contentMode: .aspectFit, options: nil) { (image, nfo) in
                        self.thirdImage.setBackgroundImage(image, for: .normal)
                    }
                default:
                    break
                }
            }
        }
        
        if camera.count > 0 {
            switch photo.count {
            case 0:
                self.setBtnImageWithCamer(start: 0, camera: camera)
            case 1:
                self.setBtnImageWithCamer(start: 1, camera: camera)
            case 2:
                self.setBtnImageWithCamer(start: 2, camera: camera)
            default:
                break
            }
        }
    }
    
    private func setBtnImageWithCamer(start: NSInteger,camera: [UIImage]) {
        switch start {
        case 0:
            for i in 0...camera.count - 1 {
                switch i {
                case 0:
                    firstImage.setBackgroundImage(camera[i], for: .normal)
                case 1:
                    secondImage.setBackgroundImage(camera[i], for: .normal)
                case 2:
                    thirdImage.setBackgroundImage(camera[i], for: .normal)
                default:
                    break
                }
            }
        case 1:
            for i in 0...camera.count - 1 {
                switch i {
                case 0:
                    secondImage.setBackgroundImage(camera[i], for: .normal)
                case 1:
                    thirdImage.setBackgroundImage(camera[i], for: .normal)
                default:
                    break
                }
            }
        case 2:
            for i in 0...camera.count - 1 {
                switch i {
                case 0:
                    thirdImage.setBackgroundImage(camera[i], for: .normal)
                default:
                    break
                }
            }
        default:
            break
        }
    }
    
    func textViewDidEndEditing(_ textView: UITextView) {
        self.delegate?.textViewText(text: textView.text)
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
